import 'package:flutter/material.dart';
import 'package:flutter_douban/config/app_colors.dart';
import 'package:flutter_douban/tool_views/back_nav_btn.dart';
import 'package:flutter_douban/tool_views/nav_title_page.dart';
import 'package:fluttertoast/fluttertoast.dart';

class FeedBackPage extends StatefulWidget {
  const FeedBackPage({Key? key}) : super(key: key);

  @override
  _FeedBackPageState createState() => _FeedBackPageState();
}

class _FeedBackPageState extends State<FeedBackPage> {
  late TextEditingController _textEditingController;
  late TextEditingController _phoneTextEditingController;
  String _textStr = '';
  String _phoneStr = '';
  FocusNode blankNode = FocusNode();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    _textEditingController = TextEditingController();
    _phoneTextEditingController = TextEditingController();
    _textEditingController.addListener(() {
      setState(() {
        _textStr = _textEditingController.text;
      });
    });

    _phoneTextEditingController.addListener(() {
      setState(() {
        _phoneStr = _phoneTextEditingController.text;
      });
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _textEditingController.dispose();
    _phoneTextEditingController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: NavTitlePage(
            titleStr: '意见反馈',
          ),
          leading: BackNavBtn(),
        ),
        body: GestureDetector(
            onTap: () {
              // 点击空白页面关闭键盘
              FocusScope.of(context).requestFocus(blankNode);
            },
            child: SingleChildScrollView(
              child: Container(
                color: Colors.white,
                child: Column(
                  children: [
                    Container(
                      height: 200,
                      color: Colors.white,
                      child: Container(
                        constraints: BoxConstraints(
                          maxHeight: 200.0,
                          minHeight: 50.0,
                        ),
                        padding: EdgeInsets.only(
                            left: 16.0, right: 16.0, top: 8.0, bottom: 4.0),
                        child: TextField(
                          controller: _textEditingController,
                          maxLines: null,
                          keyboardType: TextInputType.multiline,
                          autofocus: true,
                          decoration: InputDecoration.collapsed(
                              hintText: "请反馈您的问题,我们会尽快联系您",
                              hintStyle: TextStyle(
                                fontSize: 14,
                              )),
                        ),
                      ),
                    ),
                    Container(
                      height: 1,
                      color: Colors.grey[200],
                    ),
                    SizedBox(
                      height: 20,
                    ),
                    Container(
                      height: 60,
                      margin: EdgeInsets.only(left: 15, right: 15),
                      decoration: BoxDecoration(
                          color: Colors.grey[200],
                          borderRadius: BorderRadius.circular(6)),
                      child: Row(
                        children: [
                          Text(
                            '  手机号: ',
                            style: TextStyle(fontSize: 15, color: Colors.black),
                          ),
                          Container(
                            height: 60,
                            width: MediaQuery.of(context).size.width - 160,
                            alignment: Alignment.center,
                            child: TextField(
                              autofocus: true,
                              controller: _phoneTextEditingController,
                              decoration: InputDecoration.collapsed(
                                  hintText: '请输入手机号',
                                  hintStyle: TextStyle(
                                    fontSize: 14,
                                  )),
                            ),
                          )
                        ],
                      ),
                    ),
                    SizedBox(
                      height: 140,
                    ),
                    InkWell(
                      child: Container(
                        alignment: Alignment.center,
                        margin: EdgeInsets.only(left: 20, right: 20),
                        height: 44,
                        width: MediaQuery.of(context).size.width,
                        decoration: BoxDecoration(
                          color: (_phoneStr.length > 0 && _textStr.length > 0)
                              ? AppColors.yellow
                              : AppColors.line,
                          borderRadius: BorderRadius.circular(22),
                        ),
                        child: Text(
                          '提交',
                          style: TextStyle(
                              fontSize: 16,
                              fontWeight: FontWeight.bold,
                              color: Colors.black),
                        ),
                      ),
                      onTap: () {
                        if (_textEditingController.text.isEmpty) {
                          Fluttertoast.showToast(
                              msg: "请输入您的意见~",
                              toastLength: Toast.LENGTH_LONG,
                              gravity: ToastGravity.CENTER,
                              timeInSecForIosWeb: 1,
                              backgroundColor: Colors.black87,
                              textColor: Colors.white,
                              fontSize: 14.0);
                          return;
                        }
                        if (_phoneTextEditingController.text.isEmpty) {
                          Fluttertoast.showToast(
                              msg: "请输入您的联系方式~",
                              toastLength: Toast.LENGTH_LONG,
                              gravity: ToastGravity.CENTER,
                              timeInSecForIosWeb: 1,
                              backgroundColor: Colors.black87,
                              textColor: Colors.white,
                              fontSize: 14.0);
                          return;
                        }

                        if (_phoneTextEditingController.text.isNotEmpty &&
                            _phoneTextEditingController.text.length != 11) {
                          Fluttertoast.showToast(
                              msg: "请输入正确的联系方式~",
                              toastLength: Toast.LENGTH_LONG,
                              gravity: ToastGravity.CENTER,
                              timeInSecForIosWeb: 1,
                              backgroundColor: Colors.black87,
                              textColor: Colors.white,
                              fontSize: 14.0);
                          return;
                        }

                        if (_phoneTextEditingController.text.isNotEmpty &&
                            _phoneTextEditingController.text.length == 11) {
                          Fluttertoast.showToast(
                              msg: "提交成功~",
                              toastLength: Toast.LENGTH_LONG,
                              gravity: ToastGravity.CENTER,
                              timeInSecForIosWeb: 1,
                              backgroundColor: Colors.black87,
                              textColor: Colors.white,
                              fontSize: 14.0);
                          Navigator.pop(context);
                          return;
                        }
                      },
                    )
                  ],
                ),
              ),
            )));
  }
}
